package com.example.demo.controller;

import com.example.demo.dataobject.Complication;
import com.example.demo.dataobject.HealthInfo;
import com.example.demo.dataobject.Treatment;
import com.example.demo.dataobject.UserInfo;
import com.example.demo.repository.UserInfoRepository;
import com.example.demo.service.ComplicationService;
import com.example.demo.service.HealthInfoService;
import com.example.demo.service.TreatmentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/healthInfo")
@Slf4j
@CrossOrigin(allowCredentials = "true", allowedHeaders = "*")  //解决跨域请求,但是无法做到对于session的共享，无法针对ajax请求，所以需要对参数值进行设置
public class HealthInfoController {
    @Autowired
    private HealthInfoService healthInfoService;


    @Autowired
    private TreatmentService treatmentService;


    @Autowired
    private ComplicationService complicationService;

    @Autowired
    private UserInfoRepository userInfoRepository;


    @RequestMapping("/saveHealthInfo")
    public String saveHealthInfo(@RequestParam("telephone") String telephone,
                                 @RequestParam("identity") Integer identity,
                                 @RequestParam("sex") Integer sex,
                                 @RequestParam("birthYear") Integer birthYear,
                                 @RequestParam("birthMonth") Integer birthMonth,
                                 @RequestParam("weight") Integer weight,
                                 @RequestParam("height") Integer height,
                                 @RequestParam("laborIntensity") Integer laborIntensity,
                                 @RequestParam("diabetesType") Integer diabetesType,
                                 @RequestParam("diagnosisYear") Integer diagnosisYear,
                                 @RequestParam("diagnosisMonth") Integer diagnosisMonth,
                                 @RequestParam("treatment") Integer treatment,
                                 @RequestParam("complication") Integer complication) {

        UserInfo userInfo = userInfoRepository.findByTelephone(telephone);

        HealthInfo byTelephone = healthInfoService.findByTelephone(telephone);
        HealthInfo healthInfo = new HealthInfo(userInfo.getId(), identity, sex, birthYear, birthMonth, weight, height, laborIntensity, diabetesType, diagnosisYear, diagnosisMonth, treatment, complication);
        if (byTelephone != null) {
            healthInfo.setId(byTelephone.getId());
        }
        boolean result = healthInfoService.save(healthInfo);
        if (result) {
            return "SUCCESS";
        }
        return "ERROR";

    }



    @RequestMapping("/listHealthInfo")
    public HealthInfo listHealthInfo(@RequestParam("telephone") String telephone) {
        HealthInfo healthInfo = healthInfoService.findByTelephone(telephone);
        if (healthInfo == null) {
            return null;
        } else {
            return healthInfo;
        }
    }



    @RequestMapping("/saveComplication")
    public String saveComplication(@RequestParam("telephone") String telephone,
                                   @RequestParam("isDiabeticFootPathology") Integer isDiabeticFootPathology,
                                   @RequestParam("isDiabeticRetinopathy") Integer isDiabeticRetinopathy,
                                   @RequestParam("isDiabeticNephropathy") Integer isDiabeticNephropathy,
                                   @RequestParam("isDiabeticCardiovascularDisease") Integer isDiabeticCardiovascularDisease,
                                   @RequestParam("isDiabeticNeuropathy") Integer isDiabeticNeuropathy,
                                   @RequestParam("isDiabeticSkinLesions") Integer isDiabeticSkinLesions,
                                   @RequestParam("isHypertension") Integer isHypertension,
                                   @RequestParam("isHyperlipidemia") Integer isHyperlipidemia,
                                   @RequestParam("isOther") Integer isOther) {
        UserInfo userInfo = userInfoRepository.findByTelephone(telephone);
        Complication complication = new Complication(userInfo.getId(), isDiabeticFootPathology, isDiabeticRetinopathy, isDiabeticNephropathy, isDiabeticCardiovascularDisease, isDiabeticNeuropathy, isDiabeticSkinLesions, isHypertension, isHyperlipidemia, isOther);
        Complication byTelephone = complicationService.findByTelephone(telephone);
        // 这里是更新操作
        if (byTelephone != null) {
            complication.setId(byTelephone.getId());
        }
        boolean result = complicationService.save(complication);

        if (result) {
            return "SUCCESS";
        }
        return "ERROR";

    }



    @RequestMapping("/saveTreatment")
    public String saveTreatment(@RequestParam("telephone") String telephone,
                                @RequestParam("isDietControl") Integer isDietControl,
                                @RequestParam("isExerciseTherapy") Integer isExerciseTherapy,
                                @RequestParam("isOralMedication") Integer isOralMedication,
                                @RequestParam("isInsulin") Integer isInsulin) {
        UserInfo userInfo = userInfoRepository.findByTelephone(telephone);
        Treatment treatment = new Treatment(userInfo.getId(), isDietControl, isExerciseTherapy, isOralMedication, isInsulin);
        Treatment byTelephone = treatmentService.findByTelephone(telephone);
        if (byTelephone != null) {
            treatment.setId(byTelephone.getId());
        }

        boolean result = treatmentService.save(treatment);
        if (result) {
            return "SUCCESS";
        }
        return "ERROR";

    }



    @RequestMapping("/findHealthInfo")
    public HealthInfo findHealthInfo(@RequestParam("telephone") String telephone) {
        HealthInfo result = healthInfoService.findByTelephone(telephone);
        if (result == null) {
            return null;
        }
        return result;

    }
}
